package com.qiaofang.common.config;

import com.qiaofang.common.utils.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

@Slf4j
@Configuration
public class AuditorAwareImpl implements AuditorAware<Long> {

    /**
     * 没有测试如果createId是String类型会不会报错
     *
     * @return
     */
    @Override
    public Optional<Long> getCurrentAuditor() {
        Long userId = null;
        try {
            userId = ContextUtil.getContext().getUserId();
        } catch (Exception e) {
            log.info("自动补充创建人、修改人异常，没有登录信息");
        }
        if (userId != null) {
            return Optional.of(userId);
        } else {
            return Optional.empty();
        }
    }
}
